package com.yuyou.tas.lesson.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.tas.lesson.mapper.LessonClassStudentMapper;
import com.yuyou.tas.lesson.service.ILessonClassService;
import com.yuyou.tas.lesson.service.ILessonClassStudentService;
import com.yuyou.tas.lesson.service.IStudentService;
import com.yuyou.tas.service.api.lesson.entity.LessonClass;
import com.yuyou.tas.service.api.lesson.entity.LessonClassStudent;
import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.tool.Sequence;

@Service
public class LessonClassStudentServiceImpl implements ILessonClassStudentService{

	@Autowired
	private LessonClassStudentMapper lessonClassStudentMapper;
	
	@Autowired
	private ILessonClassService lessonClassService;
	
	@Autowired
	private IStudentService studentService;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return lessonClassStudentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByClassId7regStatus(Long classId,Integer regStatus,Long authOrganizationId){
		return lessonClassStudentMapper.deleteByClassId7regStatus(classId,regStatus,authOrganizationId);
	}
	
	@Override
	public int insert(LessonClassStudent record) {
		record.setId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		return lessonClassStudentMapper.insert(record);
	}

	@Override
	public LessonClassStudent selectByPrimaryKey(Long id) {
		return lessonClassStudentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(LessonClassStudent record) {
		return lessonClassStudentMapper.updateByPrimaryKeySelective(record);
	}

	@Override
//	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<String> sycnStuClass(List<LessonClassStudent> list) {
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		for(LessonClassStudent l:list){
			LessonClassStudent lcs=this.selectByOrgan7OId(l.getLessonClassOId(), l.getStudentOId(),l.getAuthOrganizationId());
			
			//查找相应班级
			LessonClass lc=lessonClassService.selectByOrgan7OId(l.getLessonClassOId(),l.getAuthOrganizationId());
			if(lc!=null){
				l.setLessonClassId(lc.getId());
			}
			//查找相应学生
			Student student=studentService.selectByOrgan7OId(l.getStudentOId(), l.getAuthOrganizationId());
			if(student!=null){
				l.setStudentId(student.getId());
			}
			
			if(lcs==null){
				this.insert(l);
			}else{
				lcs.setRegStatus(l.getRegStatus());
				this.updateByPrimaryKeySelective(lcs);
			}
		}	
		res.setSuccess(true);
		return res;
	}

	@Override
	public LessonClassStudent selectByOrgan7OId(Long lessonClassOId, Long studentOId,
			Long authOrganizationId) {
		return lessonClassStudentMapper.selectByOrgan7OId(lessonClassOId, studentOId, authOrganizationId);
	}

	@Override
	public ResBoolSimpleInfo<String> sycnClassStudentData(String startTime) {
				
		//查出需要更新学生字段		
		List<Student> needSycnStudentList=studentService.findNeedSycnClassStudent(startTime);
		for(Student s:needSycnStudentList){
			//更新
			this.sycnStudent(s.getId(),s.getoId(), s.getAuthOrganizationId());
		}
		
		//查出需要更新班级字段
		List<LessonClass> needSycnClassList=lessonClassService.findNeedSycnClassStudent(startTime);
		for(LessonClass lc:needSycnClassList){
			//更新
			this.sycnClass(lc.getId(), lc.getoId(), lc.getAuthOrganizationId());
		}	
		return ResBoolSimpleInfo.getSuccessInfo();
		
	}

	@Override
	public void sycnStudent(Long studentId, Long studentOId,
			Long authOrganizationId) {
		lessonClassStudentMapper.sycnStudent(studentId, studentOId, authOrganizationId);		
	}

	@Override
	public void sycnClass(Long lessonClassId, Long lessonClassOId,
			Long authOrganizationId) {
		lessonClassStudentMapper.sycnClass(lessonClassId, lessonClassOId, authOrganizationId);
	}

	@Override
	public List<LessonClassStudent> findByStudentId(Long studentId) {
		return lessonClassStudentMapper.findByStudentId(studentId);
	}

	@Override
	public Student selectUniStudent(Long authOrganizationId,
			Long lessonClassId, Long lessonClassOId, Long studentId,
			Long studentOId) {
		return lessonClassStudentMapper.selectUniStudent(authOrganizationId, lessonClassId, lessonClassOId, studentId, studentOId);
	}

	@Override
	public List<LessonClassStudent> findByStudentOId(Long studentOId) {
		return lessonClassStudentMapper.findByStudentOId(studentOId);
	}

	
	
	
	
	
	
	
	
	
	
}